const add=(a,b)=>a+b
const subtract=(a,b)=>a-b
const multiply=(a,b)=>a*b
const methods={
    add,
    subtract,
    multiply
}

const reg =/\/math\/([a-z]+)\?a=(\d+)&b=(\d+)/
/* 解析出数学方法+参数 */
const resolveUrl=(url)=>{
   const [,method,a,b]=reg.exec(url)
   return {
    method,
    a,
    b
   };
}

/* /math/add?a=2&b=3 */
function handleRequest(req,res){
    // res.end("数学服务")
    const {method,a,b}=resolveUrl(req.url)
    const ret = methods[method](a*1,b*1)

    res.end(JSON.stringify({
        method,
        a,
        b,
        ret
    }))
}

module.exports={
    handleRequest
}